<?php

/**
 * Add a new formation
 * @global type $babDB
 * @param type $name
 * @param type $levelID
 * @param type $coordinatorID
 * @param type $departementID
 * @param type $userID 
 */
function unsDepartement_addFormation($name, $levelID, $coordinatorID, $departementID, $userID) {
    global $babDB;

    if (null != $name && null != $levelID && null != $coordinatorID && null != $departementID && null != $userID) {

        global $babBody;

        $departement = unsDepartement_getDepartment($departementID);
        $groupID = bab_createGroup($name, " ", $coordinatorID, $departement['groupID']); // on crée le groupe de la formation
        
        $coordinatorGroupID = bab_createGroup('coordinator', 'Coordinator of '.$name, $coordinatorID, $groupID);
         bab_attachUserToGroup($coordinatorID, $coordinatorGroupID);

        if (0 != $groupID) {// Si != 0 alors le groupe a été crée et ça retourne l'id du group
            $babDB->db_query("INSERT INTO unsDepartement_formations (formation_name, formation_groupID, formation_coordinatorID, formation_coordinatorGroupID, formation_departementID, formation_levelID, formation_datetime, formation_author) VALUES ('" . $name . "', " . $groupID . ", " . $coordinatorID . ", " .$coordinatorGroupID. ", " . $departementID . ", " . $levelID . ", now(), " . $GLOBALS['BAB_SESS_USERID'] . ");");
        } else {
            $babBody->addError(bab_translate("A formation with this name all ready exist !", $GLOBALS['babAddonFolder']));
        }
    } else {
        $babBody->addError(bab_translate("All fields are required!", $GLOBALS['babAddonFolder']));
    }
    $idFormation = $babDB->db_insert_id();
    unsDepartement_addSemester('Semestre 1', 1, $idFormation);
    unsDepartement_addSemester('Semestre 2', 2, $idFormation);
}

function unsDepartement_editFormation($id, $name, $levelID, $coordinatorID)
{
    
    global $babDB, $babBody;

    $formation = unsDepartement_getFormation($id);
    $groupID = $formation['groupID'];
    bab_updateGroup($groupID, $name, $description);
    $babDB->db_query("update " . UNSDEPARTEMENT_FORMATIONS_TABLE_NAME . " set formation_name ='" . $name . "', formation_coordinatorID =" . $coordinatorID ." where id = " . $id);
}
    

/**
 * Return Basic information about a formation
 * @global type $babDB
 * @return Array Formations
 */

function unsDepartement_getFormationsForDepartement($id){
    global $babDB;
    
    $res = $babDB->db_query("select id, formation_name as name, formation_groupID as groupID, formation_coordinatorID as coordinatorID, formation_departementID as departementID, formation_levelID as levelID from " . UNSDEPARTEMENT_FORMATIONS_TABLE_NAME . " where formation_departementID = " . $id) ;
    
    $formations = array();
    $i = 0;
    while ($arr = $babDB->db_fetch_array($res)) {
        $formations[$i] = $arr;
        $i++;
    }
    return $formations;
}



function unsDepartement_getAllFormation() {

    global $babDB;


    $res = $babDB->db_query("select id, formation_name as name, formation_groupID as groupID, formation_coordinatorID as directorID, formation_departementID as departementID, formation_levelID as levelID from " . UNSDEPARTEMENT_FORMATIONS_TABLE_NAME);

    $formations = array();
    $i = 0;
    while ($arr = $babDB->db_fetch_array($res)) {
        $formations[$i] = $arr;
        $i++;
    }
    return $formations;
}

function unsDepartement_getAllFormationsBasic() {

    global $babDB;

    $res = $babDB->db_query("select * from " . UNSDEPARTEMENT_FORMATIONS_VIEW_NAME);

    $formations = array();
    $i = 0;
    while ($arr = $babDB->db_fetch_array($res)) {
        $formations[$i] = $arr;
        $i++;
    }
    return $formations;
}

function unsDepartement_getFormation($id) {
    global $babDB;
    
    $res = $babDB->db_query("select id, formation_name as name, formation_groupID as groupID, formation_departementID as departementID, formation_levelID as levelID, formation_coordinatorID as coordinatorID, formation_coordinatorGroupID as coordinatorGroupID from " . UNSDEPARTEMENT_FORMATIONS_TABLE_NAME . " where id = " . $id);

    $formation = $babDB->db_fetch_array($res);

    return $formation;
}


function unsDepartement_deleteFormation($id) {
    global $babDB;
    
    $listTutorialGroup = unsDepartement_getTutorialGroupsForFormation($id);
    $listTutorialLength = count($listTutorialGroup);
    
    static $k = 0;
    while ($k < $listTutorialLength) {
        unsDepartement_deleteTutorialGroup($listTutorialGroup[$k]['id']);
        $k++;
    }
    
    $semestersList = unsDepartement_getSemesterForFormation($id);
    
    foreach ($semestersList as $semester) {
        unsDepartement_deleteSemester($semester['id']);
    }
    
    
    $idFormation = $id;
    $formationGroup = unsDepartement_getFormation($idFormation);
    bab_removeGroup($formationGroup['coordinatorGroupID']);
    bab_removeGroup($formationGroup['groupID']);
    $babDB->db_query("delete from " . UNSDEPARTEMENT_FORMATIONS_TABLE_NAME . " where id = " . $idFormation . ";");
    
    
}

?>
